package com.burgeon.backend.SHOP.rest;
/**
 * openid搜索会员数据
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.User;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GETUserByOpenid extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GETUserByOpenid(String ip, String account, String password) {

        this.ip = ip;
        this.account = account;
        this.password = password;

    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public User getData(String openid) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(openid), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
           throw new Exception("获取用户错误");
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        if ("0".equals(count)){
            throw new Exception("未找道用户");
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        JSONArray array = jsonArray.getJSONArray(0);

        User user = new User();
        user.setName(array.getString(0));
        user.setPhone(array.getString(1));
        user.setIntegral(array.getString(2));
        user.setId(array.getString(3));
        user.setType(array.getString(4));
        user.setStore(array.getString(5));
        user.setCustomer(array.getString(6));
        user.setEmployee(array.getString(7));
        String u = "";
        if(array.getString(8).equals("null")){
            user.setBirthday(u);
        }else {
            user.setBirthday(array.getString(8));
        }

        user.setGender(array.getString(9));
        user.setPrice(array.getString(10));
        user.setCouQty(array.getString(11));
        user.setEmp(array.getString(12));
        user.setBalance(array.getString(13));

        user.setIsEmp(array.getString(14).equals("null")?"N":array.getString(14));
        user.setStoreId(array.getString(15));
        user.setStorejh(array.getString(16).equals("null")?"N":"Y");

        return user;
    }

    private MultiValueMap Data(String openid) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("VIPNAME");
        columns.add("CARDNO");
        columns.add("INTEGRAL");//积分
        columns.add("ID");
        columns.add("'C_VIPTYPE_ID;NAME'");//等级
        columns.add("'C_STORE_ID;NAME'");//店仓
        columns.add("OPENCARDDATE");//开卡时间
        columns.add("'HR_EMPLOYEE_ID;NAME'");//开卡人
        columns.add("BIRTHDAY");//生日
        columns.add("SEX");//性别
        columns.add("'FA_VIPACC_ID;AMOUNT'");//余额;
        columns.add("VOU_QTY");//购物券数量
        columns.add("'HR_EMPLOYEE_ID1;NAME'");//服务导购
        columns.add("CARD_REMAIN");//提货卡余额
        columns.add("'C_V_EMPLOYEE10_ID;NAME'");//绑定员工
        columns.add("'C_STORE_ID;ID'");//店仓Id
        columns.add("C_STOREJH_ID");

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        HashMap paramss = new HashMap();
        String ac = "'="+openid+"'";
        paramss.put("column", "OPENID2");
        paramss.put("condition", ac);
        params.put("params", paramss);
        params.put("count","true");

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GETUserByOpenid g = new GETUserByOpenid("39.99.239.133:55321","nea@burgeon.com.cn","burgeon55321");
        try {
            System.err.println(g.getData("1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
